package com.cmd.project.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.mail.MailUtil;
import com.cmd.common.config.CmdConfig;
import com.cmd.common.constant.Constants;
import com.cmd.common.utils.DateUtils;
import com.cmd.common.utils.StringUtils;
import com.cmd.project.domain.ProActivities;
import com.cmd.project.mapper.ProActivitiesMapper;
import com.cmd.project.service.IProActivitiesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 赛事活动Service业务层处理
 *
 * @author cq
 * @date 2025-04-03
 */
@Service
public class ProActivitiesServiceImpl implements IProActivitiesService {
    @Autowired
    private ProActivitiesMapper proActivitiesMapper;

    /**
     * 查询赛事活动
     *
     * @param id 赛事活动主键
     * @return 赛事活动
     */
    @Override
    public ProActivities selectProActivitiesById(Long id) {
        return proActivitiesMapper.selectProActivitiesById(id);
    }

    /**
     * 查询赛事活动列表
     *
     * @param proActivities 赛事活动
     * @return 赛事活动
     */
    @Override
    public List<ProActivities> selectProActivitiesList(ProActivities proActivities) {
        return proActivitiesMapper.selectProActivitiesList(proActivities);
    }

    /**
     * 新增赛事活动
     *
     * @param proActivities 赛事活动
     * @return 结果
     */
    @Override
    public int insertProActivities(ProActivities proActivities) {
        proActivities.setCreateTime(DateUtils.getNowDate());
        return proActivitiesMapper.insertProActivities(proActivities);
    }

    /**
     * 修改赛事活动
     *
     * @param proActivities 赛事活动
     * @return 结果
     */
    @Override
    public int updateProActivities(ProActivities proActivities) {
        proActivities.setUpdateTime(DateUtils.getNowDate());
        return proActivitiesMapper.updateProActivities(proActivities);
    }

    /**
     * 批量删除赛事活动
     *
     * @param ids 需要删除的赛事活动主键
     * @return 结果
     */
    @Override
    public int deleteProActivitiesByIds(Long[] ids) {
        return proActivitiesMapper.deleteProActivitiesByIds(ids);
    }

    /**
     * 删除赛事活动信息
     *
     * @param id 赛事活动主键
     * @return 结果
     */
    @Override
    public int deleteProActivitiesById(Long id) {
        return proActivitiesMapper.deleteProActivitiesById(id);
    }

    @Override
    public ProActivities getProActivity() {
        // 获取当前时间
        Date nowDate = DateUtils.getNowDate();
        return proActivitiesMapper.getProActivity(nowDate);
    }

    @Override
    public void sendEmail(Long id, String email) {

        ProActivities proActivities = proActivitiesMapper.selectProActivitiesById(id);
        String file = proActivities.getFile();
        // 将文件换成本地文件
        // 本地资源路径
        String localPath = CmdConfig.getProfile();
        String downloadPath = localPath + StringUtils.substringAfter(file, Constants.RESOURCE_PREFIX);
        MailUtil.send(email, proActivities.getActivityName(), "<h1>"+proActivities.getActivityName()+"下载报名列表</h1>", true, FileUtil.file(downloadPath));


    }
}
